package hy;

import com.netflix.client.config.IClientConfig;
import com.netflix.loadbalancer.AbstractLoadBalancerRule;
import com.netflix.loadbalancer.ILoadBalancer;
import com.netflix.loadbalancer.Server;

import java.util.List;

public class ReThreeRule extends AbstractLoadBalancerRule {
  /*每个服务执行3次*/
  private int total = 0;
  private int currentIndex = 0;	// 当前提供服务的机器号

  public Server choose(ILoadBalancer lb, Object key)
  {
      Server server = null;
      while (server == null) {
          //可正常使用的服务列表
          List<Server> upList = lb.getReachableServers();
          //所有服务列表，这里面的服务在某个时刻有可能不可用，过一会儿也许会正常
          List<Server> allList = lb.getAllServers();

          int serverCount = allList.size();
          if (serverCount == 0) {
              return null;
          }

          if(total < 3)
          {
              server = upList.get(currentIndex);
              total++;
          }else {
              total = 0;
              currentIndex++;
              if(currentIndex >= upList.size())
              {
                  currentIndex = 0;
              }
          }

          if (server == null) {
              Thread.yield();
              continue;
          }

          if (server.isAlive()) {
              return server;
          }
      }
      return server;
  }

  public Server choose(Object key) {
      return choose(getLoadBalancer(), key);
  }

  public void initWithNiwsConfig(IClientConfig clientConfig) {}
}